package com.hahaliu.hahaliu.controller.user;

import com.hahaliu.hahaliu.common.context.CurrentUser;
import com.hahaliu.hahaliu.common.exception.BaseException;
import com.hahaliu.hahaliu.common.result.Result;
import com.hahaliu.hahaliu.service.video.FavoriteService;
import com.hahaliu.hahaliu.service.video.FavoriteVideoService;
import com.hahaliu.hahaliu.service.video.UserVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.Set;

@RestController
@RequestMapping("/user/favorite/video")
@Api(tags = "用户端收藏夹视频相关接口")
@Slf4j
public class FavoriteVideoController {
	@Autowired
	private FavoriteVideoService favoriteVideoService;

	@Autowired
	private FavoriteService favoriteService;

	@Autowired
	private UserVideoService userVideoService;

	@Autowired
	private CurrentUser currentUser;

	@GetMapping("/collected-fids")
	@ApiOperation("获取用户收藏了该视频的收藏夹列表")
	public Result<Set<Integer>> getCollectedFids(@RequestParam("vid") Integer vid) {
		Integer uid = currentUser.getUserId();
		Set<Integer> fids = favoriteVideoService.findFidsOfUserFavorites(uid);
		Set<Integer> collectedFids = favoriteVideoService.findFidsOfCollected(vid, fids);
		return Result.success(collectedFids);
	}

	/**
	 * 收藏或取消收藏某视频
	 * @param vid   视频ID
	 * @param addArray  包含需要添加收藏的多个收藏夹ID组成的字符串，形式如 1,12,13,20 不能含有字符"["和"]"
	 * @param removeArray   包含需要移出收藏的多个收藏夹ID组成的字符串，形式如 1,12,13,20 不能含有字符"["和"]"
	 * @return  无
	 */
	@PostMapping("/collect")
	@ApiOperation("收藏或取消收藏某视频")
	public Result collectVideo(@RequestParam("vid") Integer vid,
	                           @RequestParam("adds") String[] addArray,
	                           @RequestParam("removes") String[] removeArray) {
		Integer uid = currentUser.getUserId();
		Set<Integer> fids = favoriteVideoService.findFidsOfUserFavorites(uid);

		Set<Integer> addSet = new HashSet<>();
		for (String add : addArray) {
			addSet.add(Integer.parseInt(add));
		}

		Set<Integer> removeSet = new HashSet<>();
		for (String remove : removeArray) {
			removeSet.add(Integer.parseInt(remove));
		}

		// 用 containsAll 判断是否有权限进行操作
		if (!fids.containsAll(addSet) || !fids.containsAll(removeSet)) {
			throw new BaseException("无权操作该收藏夹");
		}

		Set<Integer> collectedFids = favoriteVideoService.findFidsOfCollected(vid, fids);   // 原本该用户已收藏该视频的收藏夹ID集合

		// 合并 addSet 和 removeSet 的操作，避免重复
		if (!addSet.isEmpty()) {
			favoriteVideoService.addToFavorite(uid, vid, addSet);
		}
		if (!removeSet.isEmpty()) {
			favoriteVideoService.removeFromFavorite(uid, vid, removeSet);
		}

		// 判断是否为收藏或取消收藏的状态
		boolean isCollect = !addSet.isEmpty() && collectedFids.isEmpty();
		boolean isCancel = addSet.isEmpty() && !collectedFids.isEmpty() && collectedFids.equals(removeSet);

		if (isCollect) {
			userVideoService.collectOrCancel(uid, vid, true);
		} else if (isCancel) {
			userVideoService.collectOrCancel(uid, vid, false);
		}

		return Result.success();
	}

}
